﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization;
using System.Web;

namespace Sharp.Infrastructure
{
    /// <summary>
    /// 通讯上下文
    /// </summary>
    [Serializable, DataContract]
    public class ContactContext : ILogicalThreadAffinative
    {
        public const string ContextKey = "__contactContext";
        public const string ContextHeaderLocalName = "ContactContext";
        public const string ContextHeaderNamespace = "http://www.abc.com/";

        public ContactContext()
        {
        }
        protected ContactContext(SerializationInfo info, StreamingContext context)
        {
        }
        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static ContactContext Current
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Session[ContextKey] == null)
                    {
                        HttpContext.Current.Session[ContextKey] = new ContactContext();
                    }

                    return HttpContext.Current.Session[ContextKey] as ContactContext;
                }

                if (CallContext.GetData(ContextKey) == null)
                {
                    CallContext.SetData(ContextKey, new ContactContext());
                }

                return CallContext.GetData(ContextKey) as ContactContext;
            }
            set
            {
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Session[ContextKey] = value; ;
                }
                else
                {
                    CallContext.SetData(ContextKey, value);
                }
            }
        }
        /// <summary>
        /// 用户Id
        /// </summary>
        [DataMember]
        public Guid UserId
        {
            get;
            set;
        }

        /// <summary>
        /// 用户名
        /// </summary>
        [DataMember]
        public string UserName
        {
            get;
            set;
        }
        /// <summary>
        /// 访问票据
        /// </summary>
        [DataMember]
        public string AccessTicket
        {
            get;
            set;
        }
        /// <summary>
        /// Saas组织Id
        /// </summary>
        [DataMember]
        public Guid? OrgId
        {
            get;
            set;
        }
        /// <summary>
        /// 系统代码
        /// </summary>
        [DataMember]
        public string SystemCode { get; set; }
    }
}